package org.jeecg.trace.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.basic.entity.MesBomItemSetup;
import org.jeecg.basic.entity.MesLineCfg;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.prod.dto.MesWoSrcDTO;
import org.jeecg.prod.entity.MesWoSrc;
import org.jeecg.prod.service.IMesWoSrcService;
import org.jeecg.trace.entity.MesBomItemWo;
import org.jeecg.trace.service.IMesBomItemWoService;
import org.jeecg.trace.service.IMesUnitHistoryService;
import org.jeecg.trace.vo.OrderDataStatisticsVO;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "工单追溯")
@RestController
@RequestMapping("/trace/mesWoSrcTrace")
@Slf4j
public class MesWoSrcTraceController {
    @Autowired
    private IMesBomItemWoService mesBomItemWoService;

    @Autowired
    private IMesWoSrcService iMesWoSrcService;

    @Autowired
    private IMesUnitHistoryService iMesUnitHistoryService;

    /**
     * 通过主表ID查询1
     * @return
     */
    //@AutoLog(value = "工单bom从表-通过主表ID查询")
    @ApiOperation(value="工单bom表-通过主表ID查询", notes="工单bom表-通过主表ID查询")
    @GetMapping(value = "/list")
    @DS("#header.his")
    public Result<IPage<MesWoSrcDTO>> list(MesWoSrcDTO mesWoSrcDTO,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                           HttpServletRequest req) {
        Page<MesWoSrc> page = new Page<MesWoSrc>(pageNo, pageSize);
        IPage<MesWoSrcDTO> pageList = iMesWoSrcService.queryCountPageList(page, mesWoSrcDTO);
        return Result.OK(pageList);
    }

    /**
     * 通过主表ID查询1
     * @return
     */
    //@AutoLog(value = "工单bom从表-通过主表ID查询")
    @ApiOperation(value="工单bom表-通过主表ID查询", notes="工单bom表-通过主表ID查询")
    @GetMapping(value = "/listMesBomItemWoByMainId")
    @DS("#header.his")
    public Result<IPage<MesBomItemWo>> listMesBomItemWoByMainId(MesBomItemWo mesBomItemWo,
                                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                HttpServletRequest req) {
        QueryWrapper<MesBomItemWo> queryWrapper = QueryGenerator.initQueryWrapper(mesBomItemWo, req.getParameterMap());
        Page<MesBomItemWo> page = new Page<MesBomItemWo>(pageNo, pageSize);
        IPage<MesBomItemWo> pageList = mesBomItemWoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 通过主表ID查询1
     * @return
     */
    //@AutoLog(value = "工单bom从表-通过主表ID查询")
    @ApiOperation(value="工单bom表-通过主表ID查询", notes="工单bom表-通过主表ID查询")
    @GetMapping(value = "/listOrderDataStatisticsByMainId")
    @DS("#header.his")
    public Result<List<OrderDataStatisticsVO>> listOrderDataStatisticsByMainId(String woid,
                                                                               HttpServletRequest req) {
        List<OrderDataStatisticsVO> list = iMesUnitHistoryService.getOrderDataStatisticsList(woid);
        return Result.OK(list);
    }

    /**
     * 添加
     *
     * @param mesBomItemWo
     * @return
     */
    @AutoLog(value = "工单BOM-添加")
    @ApiOperation(value = "工单BOM-添加", notes = "工单BOM-添加")
    @PostMapping(value = "/addMesBomItemWo")
    @DS("business")
    public Result<String> addMesBomItemWo(@RequestBody MesBomItemWo mesBomItemWo) {
        mesBomItemWoService.save(mesBomItemWo);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mesBomItemWo
     * @return
     */
    @AutoLog(value = "工单BOM-编辑")
    @ApiOperation(value = "工单BOM-编辑", notes = "工单BOM-编辑")
    @RequestMapping(value = "/editMesBomItemWo", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("business")
    public Result<String> editMesBomItemWo(@RequestBody MesBomItemWo mesBomItemWo) {
        boolean isUpdate = mesBomItemWoService.updateByMultiId(mesBomItemWo);
//        LambdaUpdateWrapper<MesBomItemWo> updateWrapper = new LambdaUpdateWrapper<MesBomItemWo>();
//        updateWrapper.eq(MesBomItemWo::getPartno,mesBomItemWo.getPartno())
//                .eq(MesBomItemWo::getItempartno,mesBomItemWo.getItempartno())
//                .eq(MesBomItemWo::getLine,mesBomItemWo.getLine())
//                .eq(MesBomItemWo::getWoid,mesBomItemWo.getWoid())
//                .eq(MesBomItemWo::getOp,mesBomItemWo.getOp());
//        boolean isUpdate = mesBomItemWoService.update(mesBomItemWo,updateWrapper);
        return isUpdate ? Result.ok("修改成功!") : Result.error("修改失败!");
//        return Result.OK("编辑成功!");
    }

    /**
     * 通过主表ID查询1
     * @return
     */
    //@AutoLog(value = "工单bom从表-通过主表ID查询")
    @ApiOperation(value="工单bom表-通过主表ID查询", notes="工单bom表-通过主表ID查询")
    @GetMapping(value = "/exportXlsOrderData")
    @DS("#header.his")
    public ModelAndView listOrderDataStatisticsByMainId(HttpServletRequest request, String woid) {
        List<OrderDataStatisticsVO> list = iMesUnitHistoryService.getOrderDataStatisticsList(woid);
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        String title = "工单数据汇总";
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, OrderDataStatisticsVO.class);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, list);
        return mv;
    }
}
